package com.juzi.wopu.web;

import org.apache.commons.lang3.StringUtils;

import com.juzi.wopu.domain.Employee;
import com.juzi.wopu.service.IEmployeeService;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class LoginAction extends BaseAction{

	private static final long serialVersionUID = 1L;

	private IEmployeeService employeeService;
	
	private String username;
	
	private String password;

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	@Override
	public String execute() throws Exception{
		return LOGIN;
	}
	
	public void validateCheck(){
		if(StringUtils.isBlank(username)){
			addFieldError("username", "用户名不能为空");
		}
		if(StringUtils.isBlank(password)){
			addFieldError("password", "密码不能为空");
		}
	}
	
	@InputConfig(resultName=LOGIN)
	public String check() throws Exception{
		Employee loginUser = employeeService.findByLoginUser(username, password);
		if(loginUser!=null){
			setLoginUser(loginUser);
			return "main";
		}
		return LOGIN;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
